<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>接口参数编辑</title>
        <meta name="renderer" content="webkit">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
        <link rel="stylesheet" href="../../component/ui/css/ui.css"/>
    </head>
    <body>
        <form class="layui-form" action="" lay-filter="editform">
            <div class="mainBox">
                <div class="main-container">
                    <div class="main-container">
                        <div class="layui-form-item">
                            <label class="layui-form-label">名称<font color="red">*</font></label>
                            <div class="layui-input-block">
                                <input type="text" name="name" lay-verify="required|len" autocomplete="off" min="2" placeholder="请输入名称" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">唯一标志<font color="red">*</font></label>
                            <div class="layui-input-block">
                                <input type="text" name="key" lay-verify="required|len" autocomplete="off" min="2" placeholder="请输入唯一标志" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <label class="layui-form-label">类型</label>
                            <div class="layui-input-block">
                                <input type="radio" name="type" value="string" title="字符串" checked>
                                <input type="radio" name="type" value="number" title="数字">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">是否必填</label>
                            <div class="layui-input-block">
                                <input type="radio" name="required" value="1" title="是" checked>
                                <input type="radio" name="required" value="0" title="否">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">格式</label>
                            <div class="layui-input-block">
                                <select name="format">
                                    <option value="">无</option>
                                    <option value="email">邮箱</option>
                                    <option value="mobile">手机号</option>
                                    <option value="url">网址</option>
                                    <option value="image">图片</option>
                                    <option value="date">日期</option>
                                    <option value="decimal">小数</option>
                                    <option value="identityCard">身份证</option>
                                </select>
                            </div>
                        </div>
                       
                         <div class="layui-form-item">
                            <label class="layui-form-label">默认值</label>
                            <div class="layui-input-block">
                                <input type="text" name="def" autocomplete="off" placeholder="请输入默认值" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">字符长短</label>
                            <div class="layui-input-inline">
                                <input type="text" name="num_min" placeholder="最短" class="layui-input">
                            </div>
                            <div class="layui-input-inline">
                                <input type="text" name="num_max" placeholder="最长" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">数值大小</label>
                            <div class="layui-input-inline">
                                <input type="text" name="len_min" placeholder="最小" class="layui-input">
                            </div>
                            <div class="layui-input-inline">
                                <input type="text" name="len_max" placeholder="最大" class="layui-input">
                            </div>
                        </div>

                    </div>
                </div>
            </div>
            <div class="bottom">
                <div class="button-container">
                    <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit="" lay-filter="api-save">
                        <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
                </div>
            </div>
        </form>
        <script src="../../config/config.js"></script>
        <script src="../../component/layui/layui.js"></script>
        <script src="../../component/ui/ui.js"></script>
        <script>
            layui.use([
                'form', 'layer'
            ], function () {
                let form = layui.form,
                    layer = layui.layer,
                    $ = layui.$,
                    req = _req();
                form.verify({
                    len: (val, item) => {
                        let min = item.getAttribute("min")
                        if (val.length < min) {
                            return '长度不能小于' + min;
                        }
                    }
                })
                if(req.id) {
                    _get(layui, 'apiparams/editBefore?id=' + req.id, res => {
                        form.val('editform',res)
                    });
                }
                 
                //监听提交
                form.on('submit(api-save)', function (data) {
                    data = data.field;
                    data.aid = req.aid;
                    let postUrl = 'apiparams/add';
                    if(req.id) {
                        postUrl = 'apiparams/edit';
                        data.id = req.id;
                    }
                    _post(layui, postUrl, data, res => {
                        //console.log(res)
                        parent
                            .layer
                            .close(parent.layer.getFrameIndex(window.name)); //关闭当前页
                        parent.location.reload();

                    })

                    return false;
                });

            });
        </script>

    </body>
</html>